package com.example.oasystem.controller.task;

import com.example.oasystem.dao.task.AoaTaskListDao;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.task.AoaTaskList;
import com.example.oasystem.entity.task.AoaTaskLogger;
import com.example.oasystem.entity.task.AoaTaskUser;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.impl.MyTaskServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Controller
public class MytaskController {

    @Autowired
    MyTaskServiceImpl myTaskService;
    @Autowired
    AoaTaskListDao taskListDao;
    @RequestMapping("/mytask")
    public String mytask(Model model, HttpSession session,
                         @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                         @RequestParam(value = "baseKey",defaultValue = "")String baseKey,
                         @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        AoaUser user = (AoaUser)session.getAttribute("user");
        PageInfo<AoaTaskList> allTask = myTaskService.getMyTaskLimitAndBaseKey(Math.toIntExact(user.getUserId()),pageNum,pageSize,baseKey);
        List<AoaTaskList> taskLists = allTask.getList();
        for (AoaTaskList taskList : taskLists) {
            System.out.println("taskList = " + taskList);
        }

        Page page=new Page(allTask.getSize(), Math.toIntExact(allTask.getTotal()),allTask.getPages(),allTask.getPageNum(),allTask.isIsFirstPage(),allTask.isIsLastPage());
        model.addAttribute("page", page);
        model.addAttribute("tasklist", taskLists);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url", "mytaskPaging");
        return "task/mytask";
    }
    @RequestMapping("/mytaskPaging")
    public String mytaskPaging(Model model, HttpSession session,
                               @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                               @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                         @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        AoaUser user = (AoaUser)session.getAttribute("user");
        PageInfo<AoaTaskList> allTask = myTaskService.getMyTaskLimitAndBaseKey(Math.toIntExact(user.getUserId()),pageNum,pageSize,baseKey);
        List<AoaTaskList> taskLists = allTask.getList();
        for (AoaTaskList taskList : taskLists) {
            System.out.println("taskList = " + taskList);
        }

        Page page=new Page(allTask.getSize(), Math.toIntExact(allTask.getTotal()),allTask.getPages(),allTask.getPageNum(),allTask.isIsFirstPage(),allTask.isIsLastPage());
        model.addAttribute("page", page);
        model.addAttribute("tasklist", taskLists);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url", "mytaskPaging");
        return "task/mytasklist";
    }
    @RequestMapping("/myseetasks")
    public String myseetasks(@RequestParam(value = "id",defaultValue = "0") int taskId,
                             @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                             @RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                             Model model,HttpSession session){
        AoaUser user = (AoaUser)session.getAttribute("user");
        AoaTaskList task = taskListDao.selectOne(Math.toIntExact(user.getUserId()), taskId);
        System.out.println(task);
        List<AoaTaskLogger> allLogger = taskListDao.findAllLogger(taskId);
        List<AoaStatusList> allStatus = taskListDao.findAllStatus();
        List<AoaStatusList> allStatus2 = taskListDao.findAllStatus2();
        model.addAttribute("status",allStatus2);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("statuslist",allStatus);
        model.addAttribute("loggerlist",allLogger);
        model.addAttribute("task",task);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("taskId",taskId);
        return "task/myseetask";
    }
    @RequestMapping("/uplogger")
    public String uplogger(@RequestParam(value = "loggerStatusid",defaultValue = "-1")int loggerStatusid,
                           @RequestParam(value = "oldloggerStatusid",defaultValue = "-1")int oldloggerStatusid,
                           @RequestParam(value = "loggerTicking",defaultValue = "")String loggerTicking,
                           @RequestParam(value = "taskId",defaultValue = "0")int taskId,
                           @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                           @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                           Model model,HttpSession session){
        AoaUser user = (AoaUser)session.getAttribute("user");
        System.out.println("loggerStatusid = " + loggerStatusid);
        System.out.println("oldloggerStatusid = " + oldloggerStatusid);
        System.out.println("loggerTicking = " + loggerTicking);
        System.out.println("taskId = " + taskId);
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//当前时间戳
        String attendsTime = df.format(date);
        if (oldloggerStatusid==loggerStatusid&&loggerTicking.equals(""))
        {
            System.out.println("什么都没改");
            return "forward:/mytask?pageNum="+pageNum+"&baseKey="+baseKey;
        }
        else
        {
            taskListDao.insertLogger(attendsTime,loggerStatusid,loggerTicking,taskId,user.getUserName());
            taskListDao.updateTaskUser(loggerStatusid,taskId,user.getUserId()+"");
            List<AoaTaskUser> taskUserList= taskListDao.findAllTaskUserByTaskId(taskId+"");

            Integer[] arr=new Integer[taskUserList.size()];
            for (int i = 0; i < taskUserList.size(); i++) {
                AoaTaskUser taskuserMin=taskUserList.get(i);
                arr[i]= Math.toIntExact(taskuserMin.getStatusId());
            }
            int Statusid= Collections.min(Arrays.asList(arr));
            taskListDao.updateTaskList(Statusid,taskId);
            System.out.println("改了");
            return "forward:/mytask?pageNum="+pageNum+"&baseKey="+baseKey;
        }
    }
    @RequestMapping("/myshanchu")
    public String myshanchu(@RequestParam(value = "id")int id, HttpSession session, Model model,
                            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "8") int pageSize){
        AoaUser user = (AoaUser)session.getAttribute("user");
        taskListDao.delTask(id,user.getUserId());
        return "forward:/mytask?pageNum="+pageNum;
    }
}
